<template>
    <div class="switchinggroup" :key="skey">
        <div class="switchinggroupItem" v-for="(item, index) in data" :key="index">
            <div class="itemList" v-for="(list, li) in item.item" :key="li">
                <div class="name">{{ list.name }}：</div>
                <switchs v-model="list.value" :data="data" :item="list" v-if="list.type == 'switch'" @change="change"></switchs>
                <span v-else>{{ list.value }}</span>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { reactive, ref } from "vue";
import request from "@/utils/request";
import getUrlParameter from "@/utils/urlparameter";
import switchs from "@/components/switchs.vue";
import { judge } from "@/utils/switch";
import { useStore } from "@/stores";
const store = useStore();
const skey = ref(0);
const datalist: any = ref([]);
const data = reactive([
    {
        item: [
            { name: "电源总闸", value: "0", io: 0, type: "switch" },
            { name: "清水供应", value: "0", io: 1, type: "switch" },
            { name: "肥水供应", value: "0", io: 2, type: "switch" },
        ],
    },
    {
        item: [
            { name: "母液供应", value: "0", io: 3, type: "switch" },
            { name: "空气泵启动", value: "0", io: 4, type: "switch" },
            { name: "高压空气开关", value: "0", io: 5, type: "switch" },
        ],
    },
    {
        item: [
            { name: "低压空气开关", value: "0", io: 6, type: "switch" },
            { name: "高压泵启动", value: "0", io: 7, type: "switch" },
            { name: "高压泄压开关", value: "0", io: 8, type: "switch" },
        ],
    },
    {
        item: [
            { name: "滴灌泵启动", value: "0", io: 9, type: "switch" },
            { name: "滴灌泄压开关", value: "0", io: 10, type: "switch" },
            { name: "砂石过滤器排水", value: "0", io: 11, type: "switch" },
        ],
    },
    {
        item: [
            { name: "反冲洗启动", value: "关闭", io: 12, type: "text" },
            { name: "清水泵启动", value: "关闭", io: 13, type: "text" },
            { name: "配比供应", value: "关闭", io: 14, type: "text" },
        ],
    },
]);
// // 获取电源总闸状态
// const dyState = () => {
//     request.post(`/modbus-api/deviceOperate/getDiByUidIo0?addr=${getUrlParameter("id")}`).then((res: any) => {
//         data[0].item[0].value = res.msg;
//         skey.value = new Date().getTime();
//     });
// };
// // 获取清水供应状态
// const qsState = () => {
//     request.post(`/modbus-api/deviceOperate/getDiByUidQsfs?addr=${getUrlParameter("id")}&type=1`).then((res: any) => {
//         data[0].item[1].value = res.msg;
//         skey.value = new Date().getTime();
//     });
// };
// // 获取肥水供应状态
// const fsState = () => {
//     request.post(`/modbus-api/deviceOperate/getDiByUidQsfs?addr=${getUrlParameter("id")}&type=2`).then((res: any) => {
//         data[0].item[2].value = res.msg;
//         skey.value = new Date().getTime();
//     });
// };
// // 获取母液供应状态
// const myState = () => {
//     request.get(`/modbus-api/deviceOperate/getDiByUidAndOperate?addr=${getUrlParameter("id")}&operate=DO4`).then((res: any) => {
//         data[1].item[0].value = res.msg;
//         skey.value = new Date().getTime();
//     });
// };
// // 获取空气泵状态
// const kqbState = () => {
//     request.get(`/modbus-api/deviceOperate/getDiByUidAndOperate?addr=${getUrlParameter("id")}&operate=DO5`).then((res: any) => {
//         data[1].item[1].value = res.msg;
//         skey.value = new Date().getTime();
//     });
// };
// // 获取高压空气状态
// const gykqState = () => {
//     request.get(`/modbus-api/deviceOperate/getDiByUidAndOperate?addr=${getUrlParameter("id")}&operate=DO6`).then((res: any) => {
//         data[1].item[2].value = res.msg;
//         skey.value = new Date().getTime();
//     });
// };
// // 获取低压空气状态
// const dykqState = () => {
//     request.get(`/modbus-api/deviceOperate/getDiByUidAndOperate?addr=${getUrlParameter("id")}&operate=DO7`).then((res: any) => {
//         data[2].item[0].value = res.msg;
//         skey.value = new Date().getTime();
//     });
// };
// // 获取高压泵状态
// const gybState = () => {
//     request.get(`/modbus-api/deviceOperate/getDiByUidAndOperate?addr=${getUrlParameter("id")}&operate=DO8`).then((res: any) => {
//         data[2].item[1].value = res.msg;
//         skey.value = new Date().getTime();
//     });
// };
// // 获取高压泄压状态
// const gyxyState = () => {
//     request.get(`/modbus-api/deviceOperate/getDiByUidAndOperate?addr=${getUrlParameter("id")}&operate=DO9`).then((res: any) => {
//         data[2].item[2].value = res.msg;
//         skey.value = new Date().getTime();
//     });
// };
// // 获取滴灌泵状态
// const dgbState = () => {
//     request.post(`/modbus-api/deviceOperate/getDiByUid?addr=${getUrlParameter("id")}`).then((res: any) => {
//         data[3].item[0].value = res.msg;
//         skey.value = new Date().getTime();
//     });
// };
// // 获取滴灌泄压状态
// const dgxyState = () => {
//     request.get(`/modbus-api/deviceOperate/getDiByUidAndOperate?addr=${getUrlParameter("id")}&operate=DO11`).then((res: any) => {
//         data[3].item[1].value = res.msg;
//         skey.value = new Date().getTime();
//     });
// };
// // 获取砂石过滤器状态
// const ssglqState = () => {
//     request.get(`/modbus-api/deviceOperate/getDiByUidAndOperate?addr=${getUrlParameter("id")}&operate=DO12`).then((res: any) => {
//         data[3].item[2].value = res.msg;
//         skey.value = new Date().getTime();
//     });
// };
// // 获取反冲洗状态
// const fcxState = () => {
//     request.post(`/modbus-api/deviceOperate/getDiByUidFcxqdzt?addr=${getUrlParameter("id")}`).then((res: any) => {
//         data[4].item[0].value = res.msg == "1" ? "开启" : "关闭";
//         skey.value = new Date().getTime();
//     });
// };
// // 获取清水泵状态
// const qsbState = () => {
//     request.post(`/modbus-api/deviceOperate/getDiByUidQsbqdzt?addr=${getUrlParameter("id")}`).then((res: any) => {
//         data[4].item[1].value = res.msg == "1" ? "开启" : "关闭";
//         skey.value = new Date().getTime();
//     });
// };
// // 获取配比供应状态
// const pbgyState = () => {
//     request.post(`/modbus-api/deviceOperate/getDiByUidPbgy?addr=${getUrlParameter("id")}`).then((res: any) => {
//         data[4].item[2].value = res.msg == "1" ? "开启" : "关闭";
//         skey.value = new Date().getTime();
//     });
// };

const getPageData = () => {
    // //
    // dyState();
    // qsState();
    // fsState();
    // //
    // myState();
    // kqbState();
    // gykqState();
    // ///
    // dykqState();
    // gybState();
    // gyxyState();
    // //
    // dgbState();
    // dgxyState();
    // ssglqState();
    // //
    // fcxState();
    // qsbState();
    // pbgyState();
    // console.log("我要开始调接口了", datalist);
    request.get(`/modbus-api/deviceOperate/getDIStatusByUid?addr=${getUrlParameter("id")}`).then((res: any) => {
        console.log(res, "000000000000000000");
        if (res.data && res.data.length > 0) {
            data.forEach((d: any) => {
                d.item.forEach((it: any) => {
                    res.data.forEach((re: any) => {
                        if (it.io == re.io) {
                            it.value = re.status;
                            if (it.io >= 12) {
                                it.value = re.status == "0" ? "关闭" : "打开";
                            }
                        }
                        if (re.io == 15) {
                            // console.log(re, "io=15");
                            store.setState(re.status);
                        }
                    });
                });
            });
        }
        console.log(data, "开关状态集合");
        console.log(store.getState);
    });
};
setTimeout(() => {
    getPageData();
}, 1000);
const change = () => {
    datalist.value = [];
    setList();
    setTimeout(() => {
        judge(datalist.value);
    }, 1000);
};
const setList = () => {
    data.forEach((v: any, i: any) => {
        if (i < data.length - 1) {
            v.item.forEach((d: any) => {
                datalist.value.push(d.value);
            });
        }
    });
};
</script>
